package ObserverMod;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class code {
    // 抽象主题类
    interface Shower {
        void update(Editor editor);
    }

    // 抽象观察者类
    abstract class Editor {
        protected List<Shower> showers = new ArrayList<>();

        public void addShower(Shower shower) {
            showers.add(shower);
        }

        public void removeShower(Shower shower) {
            showers.remove(shower);
        }

        public void notifyShowers() {
            for (Shower shower : showers) {
                shower.update(this);
            }
        }

        public abstract List<String> getContent();

        public abstract void doChange();
    }

    // 文本编辑器类
    class TextEditor extends Editor {
        private List<String> text;

        public TextEditor() {
            this.text = new ArrayList<>();
        }

        public void setText(List<String> text) {
            this.text = text;
            doChange();
        }

        @Override
        public List<String> getContent() {
            return text;
        }

        @Override
        public void doChange() {
            notifyShowers();
        }
    }

    // 具体观察者类：字符计数显示
    class CharShower implements Shower {
        private int charCount = 0;
        private int wordCount = 0;

        @Override
        public void update(Editor editor) {
            List<String> content = ((TextEditor) editor).getContent();
            charCount = 0;
            wordCount = 0;
            for (String s : content) {
                charCount += s.length();
                wordCount += s.split("\\s+").length;
            }
            System.out.println("字符总数: " + charCount + ", 单词总数: " + wordCount);
        }
    }

    // 具体观察者类：单词列表显示
    class WordListShower implements Shower {
        private List<String> words;

        @Override
        public void update(Editor editor) {
            List<String> content = ((TextEditor) editor).getContent();
            Map<String, Integer> wordMap = new HashMap<>();
            for (String s : content) {
                for (String word : s.split("\\s+")) {
                    wordMap.put(word, wordMap.getOrDefault(word, 0) + 1);
                }
            }
            words = new ArrayList<>(wordMap.keySet());
            Collections.sort(words);
            System.out.println("单词列表: " + words);
        }
    }

    // 具体观察者类：单词频次显示
    class WordFrequencyShower implements Shower {
        private Map<String, Integer> wordFrequency;

        @Override
        public void update(Editor editor) {
            List<String> content = ((TextEditor) editor).getContent();
            wordFrequency = new HashMap<>();
            for (String s : content) {
                for (String word : s.split("\\s+")) {
                    wordFrequency.put(word, wordFrequency.getOrDefault(word, 0) + 1);
                }
            }
            System.out.println("单词频次: " + wordFrequency);
        }
    }

}
